package com.hzjq.core.impl

import com.hzjq.core.ErrorCode
import com.hzjq.core.bean.CapEntity
import com.hzjq.core.bean.CapProgressEntity
import com.hzjq.core.bean.ErrorReadCapEntity
import com.hzjq.core.bean.ErrorResult
import com.hzjq.core.callback.Callback
import com.hzjq.core.callback.OnUnderCapCallback
import com.hzjq.core.callback.ProgressCallback
import com.hzjq.core.loader.OnUnderCapLoader
import com.hzjq.core.work.Works
import com.hzjq.core.worker.*
import io.reactivex.functions.Consumer

class UnderCapImpl : OnUnderCapLoader {

    private var works: Works? = null

    override fun onUnderCap(
        caps: MutableList<CapEntity>,
        callback: OnUnderCapCallback
    ) {
        CheckerImpl().setCallback(Consumer {
            if (it) {
                cancel()
                val capCallback = object : Callback<MutableList<CapEntity>> {
                    override fun onResult(t: MutableList<CapEntity>) {
                        if (t[t.size - 1].isScanEnd) {
                            callback.onResult(ErrorReadCapEntity())
                        }
                    }

                    override fun onRetryCountChanged(retryCount: Int, action: String) {
                        callback.onRetryCountChanged(retryCount, action)
                    }

                    override fun onError(errorCode: ErrorResult) {
                        callback.onError(errorCode)
                    }
                }

                val meCallback = object : ProgressCallback<CapProgressEntity> {
                    override fun onResult(t: CapProgressEntity) {
                        callback.notifyVoltageElectricChanged(t.mVoltage, t.mElectric)
                    }

                    override fun onRetryCountChanged(retryCount: Int, action: String) {

                    }

                    override fun onError(errorCode: ErrorResult) {

                    }

                    override fun onProgressChanged(progress: Int, total: Int, action: String) {

                    }

                }

                works = Works.Builder.newBuilder()
                    .addWork(CheckCapPassWordWork(caps, callback))
                    .addWork(ClearChipStateWork(caps, capCallback))
                    .addWork(UnderCapWork(callback))
                    .addWork(UnderInnerScanModeWork(caps, capCallback))
                    .addWork(UnderScanShipWork(meCallback, callback, capCallback))
                    .addWork(ReadCapWork(capCallback))
                    .build()
                    .queue()

            } else {
                callback.onError(ErrorCode.getErrorResult(103))
            }
        }).start()
    }

    override fun onExitUnder(callback: Callback<Boolean>) {
        CheckerImpl().setCallback(Consumer {
            if (it) {
                BackWork(callback).doWork()
                works?.onDestroy()
            } else {
                callback.onError(ErrorCode.getErrorResult(103))
            }
        }).start()
    }


    override fun cancel() {
        works?.onDestroy()
    }
}